package com.esm.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.esm.dao.UserDao;
import com.esm.model.User;
import com.esm.utils.JDBCUtil;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;




/**
 * 
 * @author 王文成
 *
 */
public class UserDaoImpl implements UserDao{
	
	
	public User findUser(String uname, String upsw) 
	{
		//生成一个用户对象
		User user = new User();
		//数据库操作sql数据
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT * FROM ems_user");
		sql.append(" WHERE ems_user.uname=?");
		sql.append(" AND ems_user.upsw=?");
		//定义数据库连接对象
		Connection con = null;
		//定义预编译对象
		PreparedStatement pst = null;
		try 
		{
			//连接数据库
			con = JDBCUtil.getConnection();
			//生成预编译对象
			pst = con.prepareStatement(sql.toString());
			//给sql语句赋值ֵ
			pst.setString(1,uname);
			pst.setString(2,upsw);
			//数据库执行查询语句
			ResultSet rs = pst.executeQuery();
			if(rs.next())
			{
				//初始化用户对象参数

				user.setUid(rs.getInt("uid"));
				user.setUname(rs.getString("uname"));
				user.setUpsw(rs.getString("upsw"));
				user.setRid(rs.getInt("rid"));
			}
		} 
		catch (SQLException e) 
		
		{
			e.printStackTrace();
		}
		JDBCUtil.closeJDBC(pst, con);;
		return user;
	}





	@Override
	public JSONObject searchUserInfo(int uid) {
		JSONObject user=new JSONObject();
		StringBuffer sql=new StringBuffer("SELECT * FROM ems_user ");
		sql.append(" WHERE uid=?");
		Connection conn=JDBCUtil.getConnection();
		try {
			PreparedStatement ps=conn.prepareStatement(sql.toString());
			ps.setInt(1, uid);
			ResultSet rs=ps.executeQuery();
			if(rs.next()){
				user.put("uname", rs.getString("uname"));
				user.put("usex",rs.getString("usex"));
				user.put("uage", rs.getString("uage"));
				user.put("uidcard", rs.getString("uidcard"));
				user.put("utelphone", rs.getString("utelphone"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return user;
	}

	@Override
	public boolean updateSelfInfo(String uname, String usex, String uage, String utelphone, String uidcard, int uid) {
		boolean flag=false;
		StringBuffer sql=new StringBuffer("UPDATE ems_user ");
		sql.append(" SET uname=?,usex=?,uage=?,utelphone=?,uidcard=?");
		sql.append("WHERE uid=?");
		Connection conn=JDBCUtil.getConnection();
		try {
			PreparedStatement ps=conn.prepareStatement(sql.toString());
			ps.setString(1, uname);
			ps.setString(2, usex);
			ps.setString(3, uage);
			ps.setString(4, utelphone);
			ps.setString(5, uidcard);
			ps.setInt(6, uid);
			int i=ps.executeUpdate();
			if(i!=0){
				flag=true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public JSONArray getDefaultName(String rname) {
		JSONArray unames=new JSONArray();
		StringBuffer sql=new StringBuffer("SELECT ems_user.uname ,ems_user.uid");
		sql.append(" FROM ems_user");
		sql.append(" INNER JOIN ems_role");
		sql.append(" ON ems_user.rid=ems_role.rid");
		sql.append(" WHERE ems_role.rname=?");
		Connection conn=JDBCUtil.getConnection();
		try {
			PreparedStatement ps=conn.prepareStatement(sql.toString());
			ps.setString(1, rname);
			ResultSet rs=ps.executeQuery();
			while(rs.next()){
				JSONObject j=new JSONObject();
				j.put("uname", rs.getString("uname"));
				j.put("uid",rs.getInt("uid"));
				unames.add(j);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return unames;

	}
}
